Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random_clifford #13606

Merged
merged 6 commits into from
Jan 7, 2025
Merged

Conversation

alexanderivrii
Copy link
Contributor

Summary

Fixes #13590.

Details and comments

The bug was introduced when porting random_clifford to Rust in #12695.

I have added tests that ensure that for 2-qubits Cliffords, random_clifford produces Cliffords with all possible symplectic matrices (there should be 720) and all possible CX counts (should be {0, 1, 2, 3}). @ShellyGarion, can you think of other tests that we may wish to add?

@alexanderivrii alexanderivrii requested review from ShellyGarion and a team as code owners December 26, 2024 12:08
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@alexanderivrii alexanderivrii added this to the 1.3.2 milestone Dec 26, 2024
@alexanderivrii alexanderivrii added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Dec 26, 2024
Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alexanderivrii for fixing this bug!
The tests for 2-qubit Cliffords look good to me.
I wonder if we can also test larger Cliffrods, say with 3 and 4 qubits. We can't check that we generate the entire group (it's just too large), but perhaps we can check that they at least produce distinct CX counts (according to their popularity)?

See this table:

<style> </style>
Num of CNOTS 1 qubit 2 qubits 3 qubits 4 qubits
Paulis 4 16 64 256
0 6 36=6^2 216=6^3 1296=6^4
1 - 324=9*36 27*216 54*1296
2 - 324=9*36 432*216 1917*1296
3 - 36 2784*216 47904*1296
4 - - 3042*216 849438*1296
5 - - 432*216 8192583*1296
6 - - 2*216 22623581*1296
7 - - - 4809996*1296
8 - - - 31320*1296
9 - - - 6*1296
Total 24=4x6 11,520= 16x36x20 6720x216x64= 92,897,280 36,556,800x1296x256 =12,128,668,876,800

@coveralls
Copy link

coveralls commented Dec 26, 2024

Pull Request Test Coverage Report for Build 12631085532

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 34 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.008%) to 88.943%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 2 92.73%
crates/qasm2/src/parse.rs 12 96.69%
qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py 20 94.58%
Totals Coverage Status
Change from base Build 12420636821: -0.008%
Covered Lines: 79421
Relevant Lines: 89294

💛 - Coveralls

@alexanderivrii
Copy link
Contributor Author

The failing test seems unrelated to this PR: qiskit_neko.tests.machine_learning.test_neural_network_classifier.TestNeuralNetworkClassifierOnPrimitives.test_neural_network_classifier_2_aer[aer,machine_learning,terra]

@alexanderivrii
Copy link
Contributor Author

@ShellyGarion, based on your suggestion, I have added a test (see 97b3cc9) that checks that we also get all possible 2-qubit depths for random 3-qubit cliffords, also note that in this case for decomposing Cliffords we use the optimal Bravyj-Maslov algorithm, so the CX-counts are exact. The test considers 10,000 random Cliffords and is still reasonably fast, taking 1s on my laptop.

However, for 4-qubit Cliffords, the chosen decomposition method is only heuristic and we don't necessarily get the exact counts. In addition, it requires significantly more tests (and more time) to hit the low-popularity counts. I have locally verified that we seem to get the approximately expected counts, but for the reasons above I don't think we should add an explicit test for that.

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @alexanderivrii for fixing this bug!

@ShellyGarion ShellyGarion added this pull request to the merge queue Jan 7, 2025
Merged via the queue into Qiskit:main with commit 0d4ee23 Jan 7, 2025
17 checks passed
@wshanks
Copy link
Contributor

wshanks commented Jan 21, 2025

It looks like this missed Qiskit 1.3.2. Should it still be backported to 1.3?

@jakelishman
Copy link
Member

Yeah, looks like it didn't get tagged for backport despite the milestone. I'll shift to hopefully 1.3.3.

@jakelishman jakelishman added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jan 21, 2025
@jakelishman jakelishman modified the milestones: 1.3.2, 1.3.3 Jan 21, 2025
mergify bot pushed a commit that referenced this pull request Jan 21, 2025
* fix and tests

* Using a fixed rather than random clifford in one of the tests

* reno

* restoring previous docstring that was changed accidentally

* updating in-code comment

* also checking decompositions for random 3q cliffords

(cherry picked from commit 0d4ee23)
github-merge-queue bot pushed a commit that referenced this pull request Jan 21, 2025
* fix and tests

* Using a fixed rather than random clifford in one of the tests

* reno

* restoring previous docstring that was changed accidentally

* updating in-code comment

* also checking decompositions for random 3q cliffords

(cherry picked from commit 0d4ee23)

Co-authored-by: Alexander Ivrii <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

random_clifford does not produce random Cliffords on 2 qubits
9 participants